<?php

/**
 * profile short summary.
 *
 * profile description.
 *
 * @version 1.0
 * @author xianbin
 */
class profile extends Menu_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->Model('Profile_model');
        $this->load->Model('Menu_model');
        $this->load->library('ajaxMsgModel');
        $this->load->library('form_validation');
    }

    public function profileinfo()
    {
        $this->ajaxmsgmodel->Type='1';
        $this->Ajax_Check($this->ajaxmsgmodel);

        $menuid=$this->uri->segment(4);
        $data['menuid']=$menuid;
        $data['crumbs_items']=$this->get_crumbs_content_menu($menuid);
        $data['profileModel']=$this->Profile_model->get_first_row_by_menuid($menuid);
        $this->load->view('admin/profile/profileinfo.html',$data);
    }

    public function edit_profile()
    {
        $this->ajaxmsgmodel->Type='4';
        $this->Ajax_Check($this->ajaxmsgmodel);

        $profileModel=new $this->Profile_model();

        $profileModel->title=$this->input->post('title');
        $profileModel->contents=$this->input->post('contents',false);
        $profileModel->menuid=$this->input->post('menuid');
        $profileModel->keyword=$this->input->post('keyword');
        $profileModel->description=$this->input->post('description');
        $parent_menu_model=$this->Menu_model->get_menu_by_id($profileModel->menuid);
        $back_url=site_url('admin/'.$parent_menu_model->menu_control.'/'.$parent_menu_model->menu_action.($parent_menu_model->page_type==1?'list':'info').'/'.$parent_menu_model->menuid);
        if($this->input->post('mark')=="1")//修改
        {
            $profileModel->profile_id=$this->input->post('profile_id');
            $res= $this->Profile_model->update_profile($profileModel);
            if($res>0)
            {
                $this->ajaxmsgmodel->Msg='修改成功';
                $this->ajaxmsgmodel->Statu='ok';
                $this->ajaxmsgmodel->BackUrl=$back_url;
            }
            else
            {
                $this->ajaxmsgmodel->Msg='修改失败';
                $this->ajaxmsgmodel->Statu='err';
            }
        }else{//添加
            $res= $this->Profile_model->add_profile($profileModel);
            if($res>0)
            {
                $this->ajaxmsgmodel->Msg='添加成功';
                $this->ajaxmsgmodel->Statu='ok';
                $this->ajaxmsgmodel->BackUrl=$back_url;
            }
            else
            {
                $this->ajaxmsgmodel->Msg='添加失败';
                $this->ajaxmsgmodel->Statu='err';
            }
        }
        echo(json_encode($this->ajaxmsgmodel));
    }

    public function del_profile()
    {
        $this->ajaxmsgmodel->Type='4';
        $this->Ajax_Check($this->ajaxmsgmodel);

        $this->load->library('ajaxMsgModel');
        $profile_id=$this->uri->segment(4);
        $profile_model=$this->Profile_model->get_profile_by_id($profile_id);
        $parent_menu_model=$this->Menu_model->get_menu_by_id($profile_model->menuid);
        $back_url=site_url('admin/'.$parent_menu_model->menu_control.'/'.$parent_menu_model->menu_action.($parent_menu_model->page_type==1?'list':'info').'/'.$parent_menu_model->menuid);
        $res=$this->Profile_model->del_profile($profile_id);
        if($res>0)
        {
            $this->ajaxmsgmodel->Msg='删除成功';
            $this->ajaxmsgmodel->Statu='ok';
            $this->ajaxmsgmodel->BackUrl=$back_url;
        }
        else
        {
            $this->ajaxmsgmodel->Msg='删除失败';
            $this->ajaxmsgmodel->Statu='err';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }
}